package com.typing.battle.receiver;

import com.typing.battle.activity.TypingBattleChatRoomActivity;
import com.typing.battle.util.MessageParser;
import com.typing.battle.util.PreferenceMgr;
import com.typing.battle.value.MessageDTO;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

public class C2dmBroadcastReceiver extends BroadcastReceiver {

	static String registration_id = null;
	public static TypingBattleChatRoomActivity chatRoomActivity;
	public static final int TYPE_MESSAGE = 0;
	public static final int TYPE_NOTIFY = 1;
	public static final int TYPE_INVITE = 2;
	public static final int TYPE_ROOM_IN = 3;
	public static final int TYPE_ROOM_OUT = 4;
	public static final int TYPE_ROOM_BANISH = 5;	
	public static final int TYPE_TYPING_START = 6;
	public static final int TYPE_TYPING_END = 7;
	public static final int TYPE_TYPING_RESULT = 8;	
	
	private PreferenceMgr mgr;
	@Override
	public void onReceive(Context context, Intent intent) {
		if (intent.getAction().equals(
				"com.google.android.c2dm.intent.REGISTRATION")) {
			handleRegistration(context, intent);
		} else if (intent.getAction().equals(
				"com.google.android.c2dm.intent.RECEIVE")) {
			mgr = PreferenceMgr.getInstance(context);
			chatRoomActivity = TypingBattleChatRoomActivity.typingBattleActivity;
			int type = Integer.parseInt(intent.getExtras().getString("sendType"));
			String c2dm_msg = intent.getExtras().getString("msg");
			String msg_from = intent.getExtras().getString("msg_from");
			MessageDTO dto = new MessageDTO(type,c2dm_msg,msg_from,"");
			Log.d("msg",c2dm_msg);
			Log.d("msg_from",msg_from);
			Log.d("type",""+type);
			Toast toast = null;
			switch(type){
			case TYPE_MESSAGE :		
				if(!mgr.getData(PreferenceMgr.USER_EMAIL_KEY, "").equals(msg_from)){
					chatRoomActivity.setReceiveMessage(dto);
				}
				/*
					Toast toast = Toast.makeText(context, "메시지 도착!\n" + c2dm_msg,
							Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 150);
					toast.show();
					
					//TypingBattleChatRoomActivity.chatView.list.add(new String[]{msg_from,c2dm_msg,"right"});
					//TypingBattleChatRoomActivity.chatView.refreshData();
					break;
				}else{
					
				}*/		
				break;
			case TYPE_NOTIFY :
					break;
			case TYPE_INVITE :
					break;
			case TYPE_ROOM_IN :
					break;		
			case TYPE_ROOM_OUT :
					break;
			case TYPE_ROOM_BANISH :
				break;
			case TYPE_TYPING_START :
					toast = Toast.makeText(context, "타이핑시작",
							Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 150);
					toast.show();					
					chatRoomActivity.setReceiveMessage(dto);
					chatRoomActivity.startTyping(c2dm_msg,intent.getExtras().getString("sentenceId"),intent.getExtras().getString("seq"));
					Log.d("sentenceID" , ""+intent.getExtras().getString("sentenceId"));
					break;
			case TYPE_TYPING_END :					
					chatRoomActivity.endTyping();
					break;
			case TYPE_TYPING_RESULT :
					toast = Toast.makeText(context, "타이핑종료",
							Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 150);
					toast.show();
					chatRoomActivity.setReceiveMessage(dto);
					chatRoomActivity.endTyping();
					break;
			}
			/*switch(type){
				case TYPE_MESSAGE :					
					if(!mgr.getData(PreferenceMgr.USER_EMAIL_KEY, "").equals(msg_from)){
						Toast toast = Toast.makeText(context, "메시지 도착!\n" + c2dm_msg,
								Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 150);
						toast.show();
						TypingBattleChatRoomActivity.chatView.list.add(new String[]{msg_from,c2dm_msg,"right"});
						TypingBattleChatRoomActivity.chatView.refreshData();
						break;
					}else{
						
					}					
				case TYPE_NOTIFY :{
						Toast toast = Toast.makeText(context, "메시지 도착!\n" + c2dm_msg,
								Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 150);
						toast.show();
						TypingBattleChatRoomActivity.chatView.list.add(new String[]{msg_from,c2dm_msg,"right"});
						TypingBattleChatRoomActivity.chatView.refreshData();
						break;
					}
				case TYPE_INVITE :{						
						TypingBattleChatRoomActivity.chatView.list.add(new String[]{msg_from,c2dm_msg,"center"});
						TypingBattleChatRoomActivity.chatView.refreshData();
						break;
					}
				case TYPE_ROOM_IN :{						
						TypingBattleChatRoomActivity.chatView.list.add(new String[]{msg_from,c2dm_msg,"center"});
						TypingBattleChatRoomActivity.chatView.refreshData();
						break;
					}					
				case TYPE_ROOM_OUT :{						
						TypingBattleChatRoomActivity.chatView.list.add(new String[]{msg_from,c2dm_msg,"center"});
						TypingBattleChatRoomActivity.chatView.refreshData();
						break;
					}
				case TYPE_TYPING_START :{
						Toast toast = Toast.makeText(context, "타이핑시작",
								Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 150);
						toast.show();
						TypingBattleChatRoomActivity.chatView.list.add(new String[]{"Let's Typing",c2dm_msg,"center"});
						TypingBattleChatRoomActivity.chatView.refreshData();
						TypingBattleChatRoomActivity.chatView.startTyping(c2dm_msg,intent.getExtras().getString("sentenceId"),intent.getExtras().getString("seq"));
						
						Log.d("sentenceID" , ""+intent.getExtras().getString("sentenceId"));
						
						break;
					}
				case TYPE_TYPING_END :
					TypingBattleChatRoomActivity.chatView.endTyping();
					break;
				case TYPE_TYPING_RESULT :{
						Toast toast = Toast.makeText(context, "타이핑종료",
								Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 150);
						toast.show();
						TypingBattleChatRoomActivity.chatView.list.add(new String[]{"Typing Result",c2dm_msg,"center"});
						TypingBattleChatRoomActivity.chatView.refreshData();
						TypingBattleChatRoomActivity.chatView.endTyping();
						break;
					}
			}*/
			//C2dmTestActivity.chatView.list.add(new String[]{"receive",c2dm_msg,"right"});
			//C2dmTestActivity.chatView.refreshData();
		}
	}

	private void handleRegistration(Context context, Intent intent) {
		registration_id = intent.getStringExtra("registration_id");
		System.out.println("registration_id====>" + registration_id);
		System.out.println("ERROR====>" + intent.getStringExtra("error"));
		if (intent.getStringExtra("error") != null) {
			Log.d("C2DM_REGISTRATION", ">>>>>"
					+ "Registration failed, should try again later." + "<<<<<");
		} else if (intent.getStringExtra("unregistered") != null) {
			Log.d("C2DM_REGISTRATION",
					">>>>>"
							+ "unregistration done, new messages from the authorized sender will be rejected"
							+ "<<<<<");
		} else if (registration_id != null) {
			System.out.println("registration_id complete!!");
			PreferenceMgr mgr = PreferenceMgr.getInstance(context);
			mgr.setData(PreferenceMgr.REGISTRATION_KEY, registration_id);
		}
	}

}
